package com.hwj.controller;

import cn.hutool.jwt.JWT;
import com.hwj.entity.Permission;
import com.hwj.entity.Role;
import com.hwj.entity.RolePermission;
import com.hwj.entity.User;
import com.hwj.service.RolePermissionService;
import com.hwj.service.RoleService;
import com.hwj.service.UserInfoService;
import com.hwj.utils.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/userRole")
public class UserRoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    UserInfoService userInfoService;

    /*展示所有角色*/
    @GetMapping("/all")
    public JsonResponse all(){
        return JsonResponse.successResponse(0,"查询所有角色信息成功",roleService.select());
    }

    /*根据role_id查询角色*/
    @GetMapping("/{roleId}")
    public  JsonResponse queryById(@PathVariable Long roleId){
        Role role =roleService.selectByPrimaryKey(roleId);
        if(role!=null){
            return  JsonResponse.successResponse(0,"查询单个角色信息成功  ",role);
        }else {
            return  JsonResponse.failResponse(-2,"查询单个角色信息失败",role);
        }
    }

    /*
    * 角色分页
    * */
    @PostMapping("/page")
    public JsonResponse selectAllBypage(@RequestBody(required = false) Role role, @RequestParam(value = "pageNum", required = false) Integer pageNum, @RequestParam(value = "pageSize", required = false) Integer pageSize){
        return JsonResponse.successResponse(0,"分页查询权限信息成功",roleService.page(role,pageSize,pageNum));
    }

    /*删除*/
    @DeleteMapping("/{roleId}")
    public JsonResponse delete(@PathVariable Long roleId){
        return JsonResponse.successResponse(0,"删除成功",roleService.deleteByPrimaryKey(roleId));
    }

    /*新增*/
    @PostMapping("/")
    public JsonResponse add(@RequestBody Role role){
        return JsonResponse.successResponse(0,"新增成功",roleService.insertSelective(role));
    }

    /*更新*/
    @PutMapping("/")
    public JsonResponse update(@RequestBody Role role){
        return JsonResponse.successResponse(0,"更新成功",roleService.updateByPrimaryKeySelective(role));
    }

    //savePermission  @RequestBody Role role
    @PutMapping("/updatePermission")
    @Transactional
    public JsonResponse updatePermission(@RequestBody  Role role, HttpServletRequest request) {
        // 先根据角色id删除绑定的权限
        rolePermissionService.deleteByRoleId(role.getRoleId());
        // 遍历新的权限列表，重新绑定
        for (Long permissionId : role.getPermissionIds()) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getRoleId());
            rolePermission.setPermissionId(permissionId);
            rolePermissionService.insert(rolePermission);
        }

        // 判断当前登录的用户，其所属角色是否被修改，如果是，则返回true，需要重新登录。
        String token = request.getHeader("token");
        Object userid = JWT.of(token).getPayload("userId");
        User user = userInfoService.queryById(Long.parseLong(userid+""));
        Long roleId = user.getRoleId();
        if (roleId==role.getRoleId()) {
            return JsonResponse.successResponse(0,"该用户权限已经更新，请重新登陆",true);
        }
        else
        //如果不包含，则返回false，不需要重新登录。
            return JsonResponse.failResponse(0,"权限已经更新",false);

    }
}
